package com.example.myapp.ui.main.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.bumptech.glide.Glide;
import com.example.myapp.BaseActivity;
import com.example.myapp.R;
import com.example.myapp.myView.FogView;
import com.example.myapp.util.FileUtil;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class BigPicActivity extends BaseActivity {

    @BindView(R.id.image)
    ImageView image;

    int position;

    List<String> urlList;
    @BindView(R.id.button_previous)
    Button buttonPrevious;
    @BindView(R.id.button_next)
    Button buttonNext;
    @BindView(R.id.rubFog)
    FogView rubFog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_pic);
        String position1 = getIntent().getStringExtra("position");
        Serializable list = getIntent().getExtras().getSerializable("list");
        if (position1 != null) {
            position = Integer.valueOf(position1);
            urlList = (List<String>) list;
        }
        showPic();
    }

    private void showPic() {
        if (urlList == null) {
            return;
        }
        Glide.with(this)
                .load(urlList.get(position))
                .into(image);
    }

    private void showVideo() {
        if (urlList == null) {
            urlList = new ArrayList<>();
            urlList.add(FileUtil.getAppImagePath("/测试/测试",false).getAbsolutePath());
        }
        String s = urlList.get(position);
        Uri uri = Uri.parse(s);
        //调用系统自带的播放器
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "*/*");
        startActivity(intent);
    }

    @OnClick({R.id.button_previous, R.id.button_next, R.id.button_play})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button_previous:
                if (position == 0) {
                    return;
                }
                position--;
                showPic();
                break;
            case R.id.button_next:
                if (position == urlList.size() - 1) {
                    return;
                }
                position++;
                showPic();
                break;
            case R.id.button_play:
                showVideo();
                break;
        }
    }
}
